/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entities;

import company.search.interfaces.SearchRequerimetsOR;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author damfercer
 */
@Entity
@Table(name = "search_educational_info_requeriment_or_operator")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "SearchEducationalInfoRequerimentOrOperator.findAll", query = "SELECT s FROM SearchEducationalInfoRequerimentOrOperator s"),
    @NamedQuery(name = "SearchEducationalInfoRequerimentOrOperator.findByIdSearchEducationalInfoRequerimentOrOperator", query = "SELECT s FROM SearchEducationalInfoRequerimentOrOperator s WHERE s.idSearchEducationalInfoRequerimentOrOperator = :idSearchEducationalInfoRequerimentOrOperator")})
public class SearchEducationalInfoRequerimentOrOperator implements Serializable ,SearchRequerimetsOR {
    @Basic(optional = false)
    @NotNull
    @Column(name = "IMPORTANCE")
    private int importance;
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "ID_SEARCH_EDUCATIONAL_INFO_REQUERIMENT_OR_OPERATOR")
    private Integer idSearchEducationalInfoRequerimentOrOperator;
    @JoinColumn(name = "SECTOR", referencedColumnName = "ID_SECTOR")
    @ManyToOne(fetch = FetchType.LAZY)
    private Sector sector;
    @JoinColumn(name = "SEARCH", referencedColumnName = "ID_SEARCH")
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private Search search;
    @JoinColumn(name = "MINIMUM_EDUCATIONAL_LEVEL", referencedColumnName = "ID_EDUCATIONAL_LEVEL")
    @ManyToOne(fetch = FetchType.LAZY)
    private EducationalLevel minimumEducationalLevel;

    public SearchEducationalInfoRequerimentOrOperator() {
    }

    public SearchEducationalInfoRequerimentOrOperator(Integer idSearchEducationalInfoRequerimentOrOperator) {
        this.idSearchEducationalInfoRequerimentOrOperator = idSearchEducationalInfoRequerimentOrOperator;
    }

    public Integer getIdSearchEducationalInfoRequerimentOrOperator() {
        return idSearchEducationalInfoRequerimentOrOperator;
    }

    public void setIdSearchEducationalInfoRequerimentOrOperator(Integer idSearchEducationalInfoRequerimentOrOperator) {
        this.idSearchEducationalInfoRequerimentOrOperator = idSearchEducationalInfoRequerimentOrOperator;
    }

    public Sector getSector() {
        return sector;
    }

    public void setSector(Sector sector) {
        this.sector = sector;
    }

    public Search getSearch() {
        return search;
    }

    public void setSearch(Search search) {
        this.search = search;
    }

    public EducationalLevel getMinimumEducationalLevel() {
        return minimumEducationalLevel;
    }

    public void setMinimumEducationalLevel(EducationalLevel minimumEducationalLevel) {
        this.minimumEducationalLevel = minimumEducationalLevel;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idSearchEducationalInfoRequerimentOrOperator != null ? idSearchEducationalInfoRequerimentOrOperator.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        boolean sectorRes=false;
        boolean minimumEducationalLevelRes=false;
        if (!(object instanceof SearchEducationalInfoRequerimentOrOperator)) {
            return false;
        }
        SearchEducationalInfoRequerimentOrOperator other = (SearchEducationalInfoRequerimentOrOperator) object;
        
        //Sector test
        if(this.sector==null && other.getSector()==null){
            sectorRes=true;
        }
        else if(this.sector!=null && other.getSector()!=null){
            sectorRes=this.sector.equals(other.getSector());
        }
        //MinimumEducationalLevel test
        if(this.getMinimumEducationalLevel()==null && other.getMinimumEducationalLevel()==null){
            minimumEducationalLevelRes=true;
        }
        else if(this.getMinimumEducationalLevel()!=null && other.getMinimumEducationalLevel()!=null){
            minimumEducationalLevelRes=this.getMinimumEducationalLevel().equals(other.getMinimumEducationalLevel());
        }
        
        Boolean res=sectorRes && minimumEducationalLevelRes;
        return res;
    }

    @Override
    public String toString() {
        return "entities.SearchEducationalInfoRequerimentOrOperator[ idSearchEducationalInfoRequerimentOrOperator=" + idSearchEducationalInfoRequerimentOrOperator + " ]";
    }

    public int getImportance() {
        return importance;
    }

    public void setImportance(int importance) {
        this.importance = importance;
    }

    @Override
    public Integer getCandidateTotalPoints(Candidate c) {
        Integer finalPoints=0;
        Integer points;
        Integer pointsEducationalLevel=20;
        Integer pointSector=40;
        Integer requeriments=0;
        Integer candidateRequeriments;
        
        if(c.getCandidateEducationalInfoList()!=null && !c.getCandidateEducationalInfoList().isEmpty()){
            if(getMinimumEducationalLevel()!=null){
                requeriments++;
            }
            if(getSector()!=null){
                requeriments++;
            }
           
            for(CandidateEducationalInfo candidateEducationalInfo:c.getCandidateEducationalInfoList()){
                candidateRequeriments=0;
                points=0;
                if(getMinimumEducationalLevel()!=null){
                    if(candidateEducationalInfo.getEducationalLevel()!=null && candidateEducationalInfo.getEducationalLevel().getEducationalValue() >= getMinimumEducationalLevel().getEducationalValue() ){
                        points+=(pointsEducationalLevel*importance);
                        candidateRequeriments++;
                    }
                }
                if(getSector()!=null){
                    if(candidateEducationalInfo.getSector()!=null && candidateEducationalInfo.getSector().equals(getSector())){
                        points+=(pointSector*importance);
                        candidateRequeriments++;
                    }
                }
                if(requeriments<=candidateRequeriments){
                    points=points*2;
                }
                finalPoints+=points;
            }
        }
        return finalPoints;
    }
    
}
